package algos;

import java.util.List;

import ue.UE;
import eNodeB.ENodeB;
import eNodeB.RessourceBlock;

public class MaxCQI extends AAlgos {

	public MaxCQI(ENodeB eNodeB) {
		super(eNodeB);
	}

	@Override
	public void run() {
		eNodeB.freeMapRessourceBlocks();
		eNodeB.freeBusyRessourceBlocks();
		List<UE> users = eNodeB.getActualUE();
		if (!users.isEmpty()) {
			try {
				eNodeB.getSem().acquire();
			} catch (InterruptedException e1) {
				e1.printStackTrace();
			}

			while (eNodeB.getPoolRessourceBlocksSize() != 0) {
				RessourceBlock rb = eNodeB.getFirstFreeRessourceBlock();
				if (rb != null) {
					int max = 0;
					UE bestUe = users.get(0);
					for (UE ue : users) {
						int k = ue.getCQI(rb.getId());

						if (k > max) {
							max = k;
							bestUe = ue;
						}
					}
					schedule(bestUe, rb);
				}
			}
			eNodeB.getSem().release();

		}
	}
}
